﻿Imports System.Net
Imports System.IO
Imports System.Text
Imports System.Xml
Imports System.Xml.Linq.XElement


Public Class Airlines

    Dim request As HttpWebRequest
    Dim response As HttpWebResponse = Nothing
    Dim reader As StreamReader
    Dim sbSource As StringBuilder

    Private pri_num As String
    Private pri_bandara As String
    Private pri_flightnum As String
    Private pri_jam As String
    Private pri_aktual As String
    Private pri_jenis As String
    Private pri_terminal As String
    Private pri_gate As String
    Private nama_bandara As String

    Property NamaBandara As String
        Get
            Return nama_bandara
        End Get
        Set(value As String)
            nama_bandara = NamaBandara
        End Set
    End Property

    Property Num As String
        Get
            Return pri_num
        End Get
        Set(value As String)
            pri_num = Num
        End Set
    End Property

    Property Bandara As String
        Get
            Return pri_bandara
        End Get
        Set(value As String)
            pri_bandara = Bandara
        End Set
    End Property

    Property FlightNum As String
        Get
            Return pri_flightnum
        End Get
        Set(value As String)
            pri_flightnum = FlightNum
        End Set
    End Property

    Property Jam As String
        Get
            Return pri_jam
        End Get
        Set(value As String)
            pri_jam = Jam
        End Set
    End Property

    Property Aktual As String
        Get
            Return pri_aktual
        End Get
        Set(value As String)
            pri_aktual = Aktual
        End Set
    End Property

    Property Jenis As String
        Get
            Return pri_jenis
        End Get
        Set(value As String)
            pri_jenis = Jenis
        End Set
    End Property

    Property Terminal As String
        Get
            Return pri_terminal
        End Get
        Set(value As String)
            pri_terminal = Terminal
        End Set
    End Property

    Property Gate As String
        Get
            Return pri_gate
        End Get
        Set(value As String)
            pri_gate = Gate
        End Set
    End Property

    Public Function KodeAirlines(kode As String) As String
        Dim KodeString As String

        If kode = "AK" Then
            KodeString = "Air Asia"
        ElseIf kode = "CC" Then
            KodeString = "Air Atlanta"
        ElseIf kode = "CA" Then
            KodeString = "Air China"
        ElseIf kode = "AF" Then
            KodeString = "Air France"
        ElseIf kode = "AI" Then
            KodeString = "Air India"
        ElseIf kode = "S2" Then
            KodeString = "Air Sahara"
        ElseIf kode = "AP" Then
            KodeString = "Airfast Service"
        ElseIf kode = "OZ" Then
            KodeString = "Asiana Airlines"
        ElseIf kode = "SM" Then
            KodeString = "Avient Ltd"
        ElseIf kode = "BN" Then
            KodeString = "Bali International Air Service"


        ElseIf kode = "Y6" Then
            KodeString = "Batavia Air"
        ElseIf kode = "BK" Then
            KodeString = "Bayu Air Cargo"
        ElseIf kode = "OB" Then
            KodeString = "BLUE AIR"
        ElseIf kode = "BO" Then
            KodeString = "Bouraq  Indonesia Airlines"
        ElseIf kode = "BA" Then
            KodeString = "British  Airlines"
        ElseIf kode = "8F" Then
            KodeString = "Cardig Air"
        ElseIf kode = "CX" Then
            KodeString = "Cathay Pacific Airways"
        ElseIf kode = "5J" Then
            KodeString = "Cebu Pacific"
        ElseIf kode = "CI" Then
            KodeString = "China Airlines"
        ElseIf kode = "CZC" Then
            KodeString = "China Southern Airline Charter"


        ElseIf kode = "CZE" Then
            KodeString = "China Southern Airline Extra"
        ElseIf kode = "CZ" Then
            KodeString = "China Southern Airlines"
        ElseIf kode = "W2" Then
            KodeString = "Efata Papua Airlines"
        ElseIf kode = "EK" Then
            KodeString = "Emirates Air"
        ElseIf kode = "EY" Then
            KodeString = "Etihad Airways"
        ElseIf kode = "BR" Then
            KodeString = "Eva Air"
        ElseIf kode = "FX" Then
            KodeString = "Federal Express"
        ElseIf kode = "GAC" Then
            KodeString = "Garuda City link"
        ElseIf kode = "GA" Then
            KodeString = "GARUDA INDONESIA"
        ElseIf kode = "GH" Then
            KodeString = "Garuda Indonesia Haji"


        ElseIf kode = "GF" Then
            KodeString = "Gulf Air"
        ElseIf kode = "UO" Then
            KodeString = "Hongkong Express Airways"
        ElseIf kode = "FI" Then
            KodeString = "Iceland Air"
        ElseIf kode = "QZ" Then
            KodeString = "Indonesia Air Asia"
        ElseIf kode = "JO" Then
            KodeString = "JALways"
        ElseIf kode = "JL" Then
            KodeString = "Japan Airlines"
        ElseIf kode = "VJ" Then
            KodeString = "Jatayu Airlines"
        ElseIf kode = "JQ" Then
            KodeString = "Jetstar"
        ElseIf kode = "JX" Then
            KodeString = "JETT8"
        ElseIf kode = "9V" Then
            KodeString = "Jett8 Airlines Cargo"


        ElseIf kode = "3Y" Then
            KodeString = "Kartika Airlines"
        ElseIf kode = "KL" Then
            KodeString = "KLM Royal Deutch Airlines"
        ElseIf kode = "KE" Then
            KodeString = "Korean Air"
        ElseIf kode = "KU" Then
            KodeString = "Kuwait Airways"
        ElseIf kode = "JI" Then
            KodeString = "Linus Airways"
        ElseIf kode = "JT" Then
            KodeString = "Lion Airlines"
        ElseIf kode = "LH" Then
            KodeString = "Lufthansa AIrlines"
        ElseIf kode = "MH" Then
            KodeString = "Malaysia Airlines"
        ElseIf kode = "RI" Then
            KodeString = "Mandala Airlines"
        ElseIf kode = "MEGANTARA AIR" Then
            KodeString = "MEGANTARA AIR"


        ElseIf kode = "MZ" Then
            KodeString = "Merpati Nusantara"
        ElseIf kode = "WY" Then
            KodeString = "Oman Airlines"
        ElseIf kode = "OX" Then
            KodeString = "Orient Thai"
        ElseIf kode = "EP" Then
            KodeString = "Pelita Air Service"
        ElseIf kode = "PR" Then
            KodeString = "Phillipines Airlines"
        ElseIf kode = "PH" Then
            KodeString = "phuket airlines"
        ElseIf kode = "PO" Then
            KodeString = "Polar Air"
        ElseIf kode = "YQ" Then
            KodeString = "Polet Cargo Airlines"
        ElseIf kode = "PV" Then
            KodeString = "Private"
        ElseIf kode = "QF" Then
            KodeString = "Qantas Airways"


        ElseIf kode = "QR" Then
            KodeString = "Qatar Airways"
        ElseIf kode = "RP" Then
            KodeString = "Republic Express"
        ElseIf kode = "RH" Then
            KodeString = "REPUBLIK EXPRESS AIRLINES"
        ElseIf kode = "RA" Then
            KodeString = "Riau Airlines"
        ElseIf kode = "AA" Then
            KodeString = "Royal Australia Airforce"
        ElseIf kode = "BI" Then
            KodeString = "Royal Brunei Airlines"
        ElseIf kode = "RJ" Then
            KodeString = "Royal Jordanian Airline"
        ElseIf kode = "RK" Then
            KodeString = "Royal Khmer Airlines"
        ElseIf kode = "SH" Then
            KodeString = "Saudi Arabia Haji"
        ElseIf kode = "SV" Then
            KodeString = "Saudi Arabian Airlines"


        ElseIf kode = "MI" Then
            KodeString = "Silk Air"
        ElseIf kode = "SQ" Then
            KodeString = "Singapore Airline"
        ElseIf kode = "SC" Then
            KodeString = "Singapore Airline Cargo"
        ElseIf kode = "UL" Then
            KodeString = "Sri Lankan Airlines"
        ElseIf kode = "SJ" Then
            KodeString = "Sriwijaya"
        ElseIf kode = "5H" Then
            KodeString = "Star Airline"
        ElseIf kode = "FD" Then
            KodeString = "Thai Air Asia"
        ElseIf kode = "TG" Then
            KodeString = "Thai Airways"
        ElseIf kode = "TC" Then
            KodeString = "Thomas Cook"
        ElseIf kode = "TS" Then
            KodeString = "Top Air Indonesia"


        ElseIf kode = "TH" Then
            KodeString = "Transmile Airline"
        ElseIf kode = "XN" Then
            KodeString = "Travel Express Aviation Service"
        ElseIf kode = "GY" Then
            KodeString = "Tri-Mgintra Asia Airlines"
        ElseIf kode = "TN" Then
            KodeString = "Trigana Air Services"
        ElseIf kode = "VF" Then
            KodeString = "Value Air"
        ElseIf kode = "VN" Then
            KodeString = "Vietnam Airline"
        ElseIf kode = "ZG" Then
            KodeString = "Viva Macao"
        ElseIf kode = "IW" Then
            KodeString = "Wings Abadi Airlines PT"
        ElseIf kode = "IY" Then
            KodeString = "Yemenia Airways"
        ElseIf kode = "ZH" Then
            KodeString = "Zhenzhen Airlines"

        End If

        Return KodeString
    End Function

    Public Function GetFlight(domint As String, depint As String, kode As String, wait As String) As String 'List(Of Airlines)
        Dim Teks As String = ""
        Dim TeksKirim As String = ""
        Dim waktu, waktuS As String
        Dim jam, menit, jamS, menitS, selisih As Integer
        Dim ekse As Boolean = False

        Dim url As String = "http://flo.ap01.aws.af.cm/angkasaWS/" & domint & "/" & depint
        'Dim url As String = "C:\departure.xml"
        Dim proxy As WebClient = New WebClient()
        Dim hasilxml As String = proxy.DownloadString(url)
        Dim xmllinq = XDocument.Load(New StringReader(hasilxml))
        Dim dataku = From elem In xmllinq.Root.Elements("flight")
            Select elem
        Dim hasil As List(Of Airlines) = New List(Of Airlines)

        For Each i In dataku
            ekse = False
            Dim datahasil As Airlines = New Airlines()
            datahasil.pri_flightnum = i.Elements("flightnumber").Value
            datahasil.pri_jam = i.Elements("schedule").Value

            waktu = datahasil.pri_jam '19:00
            jam = CInt(Mid(waktu, 1, 2)) '19
            menit = CInt(Mid(waktu, 4, 2)) '00

            waktuS = Format(Now, "HH:mm") '22:14
            jamS = CInt(Mid(waktuS, 1, 2)) '22
            menitS = CInt(Mid(waktuS, 4, 2)) '14
            selisih = ((jam - jamS) * 60) + (menit - menitS)

            If kode = Mid(datahasil.pri_flightnum, 1, 2) Or kode = "--" Then
                If wait = "Waiting Times" Then
                    ekse = True
                ElseIf selisih >= -120 And selisih <= 120 And wait = "< 2 Jam" Then
                    ekse = True
                ElseIf selisih > 120 And selisih <= 240 And wait = "2 - 4 Jam" Then
                    ekse = True
                ElseIf selisih > 240 And selisih <= 360 And wait = "4 - 6 Jam" Then
                    ekse = True
                ElseIf selisih > 360 And selisih <= 480 And wait = "6 - 8 Jam" Then
                    ekse = True
                ElseIf selisih > 480 And selisih <= 720 And wait = "8 - 12 Jam" Then
                    ekse = True
                ElseIf selisih > 720 And wait = "> 12 Jam" Then
                    ekse = True
                End If
            End If

            If ekse = True Then
                If depint = "arrival" Then
                    datahasil.pri_bandara = i.Elements("from").Value
                Else
                    datahasil.pri_bandara = i.Elements("goingto").Value
                End If

                datahasil.pri_num = i.Elements("num").Value
                datahasil.pri_jam = i.Elements("schedule").Value
                datahasil.pri_aktual = i.Elements("actual").Value
                datahasil.pri_jenis = i.Elements("remark").Value
                datahasil.pri_terminal = i.Elements("terminal").Value
                datahasil.pri_gate = i.Elements("gate").Value

                If depint = "arrival" Then
                    Teks = "Flight " & datahasil.pri_num & "  " & KodeAirlines(kode) & "  From  " & datahasil.pri_bandara & " Flight-Number  " & datahasil.pri_flightnum & "  Schedule  " & datahasil.pri_jam & "  Actual  " &
                       datahasil.pri_aktual & "  Remark  " & datahasil.pri_jenis & "  Terminal  " & datahasil.pri_terminal & "  Gate  " & datahasil.pri_gate

                    TeksKirim = TeksKirim & "  **--**  " & Teks

                Else
                    Teks = "Flight " & datahasil.pri_num & "  " & KodeAirlines(kode) & "  Goingto  " & datahasil.pri_bandara & " Flight-Number  " & datahasil.pri_flightnum & "  Schedule  " & datahasil.pri_jam & "  Actual  " &
                       datahasil.pri_aktual & "  Remark  " & datahasil.pri_jenis & "  Terminal  " & datahasil.pri_terminal & "  Gate  " & datahasil.pri_gate

                    TeksKirim = TeksKirim & "  **--**  " & Teks
                End If
                
            End If

        Next

        Return TeksKirim
    End Function
End Class
